package org.example.blogserver.controller;

import org.example.blogserver.handler.TokenManager;
import org.example.blogserver.service.LoginService;
import org.example.blogserver.service.SysUserService;
import org.example.blogserver.vo.Result;
import org.example.blogserver.vo.params.LoginParam;
import org.example.blogserver.vo.params.PasswordParam;
import org.example.blogserver.vo.params.RegisterParam;
import org.example.blogserver.vo.params.UserInfoParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@CrossOrigin
@RequestMapping("user")
public class UsersController {

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginParam loginParam){
        return loginService.login(loginParam);
    }
    @PostMapping("/logout")
    public Result logout(@RequestHeader("token") String token){
        return loginService.logout(token);
    }
    @PostMapping("/register")
    public Result register(@RequestBody RegisterParam registerParam){
        return loginService.register(registerParam);
    }
    @PostMapping("/upload-avatar")
    public Result uploadAvatar(@RequestHeader("token") String token,@RequestBody byte[] avatar) {
        System.out.println(Arrays.toString(avatar));
        String account = tokenManager.getAccountFromToken(token);
        return sysUserService.updateAvatarByAccount(account, avatar);
    }
    @PostMapping("/update-password")
    public Result changePassword(@RequestHeader("token") String token, @RequestBody PasswordParam passwordParam) {
        System.out.println(passwordParam);
        return sysUserService.updatePassword(token,passwordParam);
    }
    @PostMapping("/update-nickname")
    public Result changeNickname(@RequestHeader("token") String token, @RequestParam String nickname) {
        return sysUserService.updateNicknameByAccount(token,nickname);
    }
    @PostMapping("/user-info")
    public Result getUserByAccount(@RequestBody UserInfoParam userInfoParam){
        System.out.println(userInfoParam.getAccount());
        return sysUserService.getUserInfoByAccount(userInfoParam.getAccount());
    }
}
